package com.sky.task;


import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 定时检查订单状态，如果订单状态处于待支付，则取消该订单
     */
    @Scheduled(cron = "0 * * * * ?")
    public void processTimeoutOrders(){
        log.info("处理超时订单，{}", LocalDateTime.now());
        List<Orders> ordersList = orderMapper
                .getByStatusAndOrderTimeLT(Orders.PENDING_PAYMENT,
                        LocalDateTime.now().plusMinutes(-15));
        if(ordersList != null && !ordersList.isEmpty()){
            for (Orders orders : ordersList) {
                orders.setStatus(Orders.CANCELLED);
                orders.setCancelReason("支付超时，取消订单");
                orders.setCancelTime(LocalDateTime.now());
                orderMapper.update(orders);
            }
        }
    }


    /**
     * 定时检查订单状态，如果订单状态处于派送中，则更新为完成
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void processDeliveryOrders(){
        log.info("处理处于派送状态的订单，{}", LocalDateTime.now());
        List<Orders> ordersList = orderMapper
                .getByStatusAndOrderTimeLT(Orders.DELIVERY_IN_PROGRESS
                        , LocalDateTime.now().plusMinutes(-60));
        if(ordersList != null && !ordersList.isEmpty()){
            for (Orders orders : ordersList) {
                orders.setStatus(Orders.COMPLETED);
                orderMapper.update(orders);
            }
        }
    }

}
